home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 172_01 / lex.c < prev    next >
Text File  |  1980-01-01  |  21KB  |  642 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     LEX - A Lexical Analyser Generator
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Jan 30, 1985
  6.   DESCRIPTION:     A Lexical Analyser Generator. From UNIX
  7.   KEYWORDS:     Lexical Analyser Generator YACC C PREP
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      LEX.C
  10.   WARNINGS:     This program is not for the casual user. It will
  11.          be useful primarily to expert developers.
  12.   CRC:         N/A
  13.   SEE-ALSO:     YACC and PREP
  14.   AUTHORS:     Scott Guthery 11100 leafwood lane Austin, TX 78750
  15.   COMPILERS:     LATTICE C 3.0h
  16.   REFERENCES:     UNIX Systems Manuals
  17. */
  18.  
  19. /*
  20.  * Copyright (c) 1978 Charles H. Forsyth
  21.  */
  22.  
  23. /*
  24.  * lex -- initialisation, allocation, set creation
  25.  *
  26.  *     Revised for PDP-11 (Decus) C by Martin Minow
  27.  */
  28.  
  29. /* Modified 02-Dec-80 Bob Denny -- Conditionalized debug code for smaller size
  30.  *                           01 -- Moved calls to dfa build, min, print, write
  31.  *                                  and to stat, and code for ending() into
  32.  *                                  this module so that 'ytab' could be put
  33.  *                                  into overlay region.
  34.  *          29-May-81 Bob Denny -- More extern hacking for RSX overlaying.
  35.  * More     19-Mar-82 Bob Denny -- New C library & compiler
  36.  * More     03-May-82 Bob Denny -- Final touches, remove unreferenced autos
  37.  *          28-Aug-82 Bob Denny -- Add "-s" switch to supress references to
  38.  *                                  "stdio.h" in generated code.  Add switch
  39.  *                                  comments in code. Add -e for "easy" com-
  40.  *                                  mand line. "lex -e file" is the short way
  41.  *                                  of saying:
  42.  *                                      "lex -i file.lxi -o file.c -t file"
  43.  * More(!)  30-Oct-82 Bob Denny -- Fix RSX ODL to put lots of FCS junk into
  44.  *                                  overlay, pick up (badly needed) 3KW for
  45.  *                                  NFA nodes, etc.  Change static allocations
  46.  *                                  in LEXLEX.H for RSX so can do non-trivial
  47.  *                                  things.  Task is now big on RSX and grows
  48.  *                                  from big to huge as it runs.
  49.  *                                 Fix "-s" support so it is again possible
  50.  *                                  to do a lexswitch() (dumb!).
  51.  *          14-Apr-83 Bob Denny    VAX-11 C workarounds.
  52.  *                                 Fix definition of toupper().
  53.  *        20-Nov-83 Scott Guthery  Adapt for IBM PC & DeSmet C
  54.  *          22-Jun-86 Andrew Ward  Adapted for Lattice C ver 3.0h.  Debug and 
  55.  *                    Error detection code installed as aid in 
  56.  *                       detection of wild pointers which result 
  57.  *                                 from interchanging pointers and integers. 
  58.  *                          The interchangable use of pointers and 
  59.  *                                 integers was eliminated. 
  60.  *
  61.  *                                 NOTICE: some Lattice 3.0 functions were used *                   However, these should be easy to recode.  
  62.  *                   Non-ANSII functions will be removed latter
  63.  *          
  64.  */
  65.  
  66. #ifdef  DOCUMENTATION
  67.  
  68. title   lex     A Lexical Analyser Generator
  69. index           A Lexical Analyser Generator
  70.  
  71. synopsis
  72.  
  73.         lex [-options] [-i grammar] [-o outfile] [-t table]
  74.  
  75. description
  76.  
  77.         Lex compiles a lexical analyser from a grammar and description of
  78.         actions.  It is described more fully in lex.doc: only usage is
  79.         described.  The following options are available:
  80.         .lm +16
  81.         .s.i-16;-a              Disable recognition of non-ASCII characters
  82.         (codes > 177 octal) for exception character classes (form [^ ...]).
  83.         .s.i-16;-d              Enable debugging code within lex.  Normally
  84.         needed only for debugging lex.
  85.         .s.i-16;-e              "Easy" command line. Saying "lex#-e#name" is the
  86.         same as saying:
  87.         .s.i 4;"lex -i name.lxi -o name.c -t name"
  88.         .s
  89.         Do not include devices or an extension on "name" or make it longer
  90.         than 8 characters, or you'll get several error messages.
  91.         .s.i-16;-i file         Read the grammar from the file.  If "-i" is not
  92.         specified, input will be read from the standard input.
  93.         .s.i-16;-m              Enable state minimization. Currently not
  94.  
  95.         implemented, switch is a no-op.
  96.         .s.i-16;-o file         Write the output to the file.  If "-o" is not
  97.         specified, output will be written to file "lextab.c".
  98.         .s.i-16;-s              "Stand-alone" switch.  Supresses the line
  99.         "#include <stdio.h>" normally generated in the lex output.  Use this
  100.         if LEX is generating a module to be used in a program which does not
  101.         use the "standard I/O" package.
  102.         .s.i-16;-t table        Name the recognizer "table" instead of the
  103.         default "lextab".  If -o is not given, output will be written to file
  104.         "table.c".
  105.         .s.i-16;-v [file]       Verify -- write internal tables to the
  106.         indicated file.  If "-v" is given without a file name argument,
  107.         tables will be written to "lex.out".
  108.         .lm -16
  109.  
  110. diagnostics
  111.  
  112.         The following error messages may occur on invocation.  See lex
  113.         documentation for information on compilation errors.
  114.         .lm +8
  115.         .s.i -8;Can't create ...
  116.         .s.i -8;Cannot open ...
  117.         .s.i -8;Illegal option.
  118.         .s.i -8;Illegal switch combination.
  119.         .s
  120.         "-i", "-o" or "-t" given with "-e" or vice-versa
  121.         .s.i -8;Table name too long.
  122.         .s
  123.         The table name (argument to "-t") must not be longer than 8 bytes.
  124.         .s.i -8;Missing table name.
  125.         .s.i -8;Missing input file.
  126.         .s.i -8;Missing output file.
  127.         .s.i -8;Missing name.
  128.         .lm -8
  129.  
  130. author
  131.         Charles Forsyth
  132.         Modified by Martin Minnow, Bob Denny & Scott Guthery
  133.         Modified by Andrew M. Ward, Jr.
  134. bugs
  135.  
  136.  
  137. #endif
  138.  
  139. /*
  140.  * LEX -- Lexical scanner acquired from C-users group.
  141.  *      In original form this program was an example of what good C
  142.  *      programing is NOT.  I have tried to eliminate machine dependancies
  143.  *      where ever possible.  In particular, the interchangable use of
  144.  *      pointers and ints has been eliminated.  External fuctions are declared
  145.  *         with argument typed.
  146.  *  1986
  147.  * 02 June AMW: installed Lattice 3.0 functions in place of some combersom routines
  148.  *         used by original code.
  149.  * 04 June routine lexsort: qsort replaced with call to Lattice function of same name.
  150.  *         Result: LEX produces output with proper appearance, but does not
  151.  *         compile correctly.  Dangaling pointer is cause.
  152.  * 22 June My substitution code for NEWCCL was faulty.  Original code replaced
  153.  *         and system behaved properly.  Further, using C-terp it was determined
  154.  *         that the routine lexswitch returned the wrong value, this was corrected.
  155.  * 23 June modifications made to DEBUG segments.  Program LEX produces compilable
  156.  *         output that functions for word.lxi -> word.c as intended.  hword.lxi
  157.  *         does not process to completion.  Symptom: lex hangs-up while displaying
  158.  *         XX nets NN.  The offending code is the DEBUG segment and the surrounding
  159.  *         loop.  Assert lines were installed but detected no errors.
  160.  * 24 June Newset modified to use sqsort() when routine called for DFA request.
  161.  *         This modification may improve execution speed.
  162.  * 25 June The program now compiles and produces functional code for hword.lxi
  163.  *         and word.lxi.  Both programs fuction correctly.  Further, I uncovered
  164.  *         an error in the debug code I installed.  It appears that certain control
  165.  *         codes disable ANSI.SYS and the display can not be restarted.  The code
  166.  *         is in BASE.C and is identified by the UDEBUG segment.
  167.  *
  168.  */
  169. #include <stdio.h>
  170. #include "lexlex.h"  
  171.  
  172. #ifdef LATTICE
  173. #include <assert.h>
  174. #include <stdlib.h>
  175. #include <string.h>
  176. #include <dos.h>
  177.  
  178. /* Set memory size for procedure.  Lattic